<?php
namespace app\common\Controller;

use think\Controller;
use think\facade\Session;
use think\facade\Cache;
use app\common\model\Cate;
use app\common\model\User;
use app\common\model\System;

class Base extends Controller
{
    // 初始化 创建常量 公共方法 在所有方法前被调用
    protected function initialize()
    {
		parent::initialize();
		//系统配置
		$this->showSystem();
        //显示分类导航
        $this->showNav();
		$this->showUser();
		
    }
	
	//判断是否已登录？
	protected function isLogged()
	{
		if(Session::has('user_id')){
			$this->success('您已登录','index/index/index');
		}
	}

    //判断是否需要登录？
    protected function isLogin()
    {
        if(!Session::has('user_id')){
            $this->error('请登录','index/user/login');
        }
    }
	
	 //判断密码找回是否已进行了邮件发送？
    protected function isMailed()
    {
        if(!Cache::has('userid')){
            $this->error('错误请求，请正确操作！','index/user/forget');
        }
    }

    //显示导航
    protected function showNav()
    {
        //1.查询分类表获取所有分类
		$cateList = Cate::where('status',1)->order('sort','asc')->select();
		
        //2.将catelist变量赋给模板 公共模板nav.html
        $this->view->assign('cateList',$cateList);

    }
	
	//显示用户
    protected function showUser()
    {
		$user['user_id'] = Session::get('user_id');
        //1.查询用户
		$user = User::where('id',$user['user_id'])->find();
	
        //2.将User变量赋给模板 公共模板nav.html
        $this->view->assign('user',$user);

    }
	
	 //显示网站设置
    protected function showSystem()
    {
        //1.查询分类表获取所有分类
		$sysInfo = System::find(1);
		if($sysInfo['is_open'] ==1){
			$this->assign('sysInfo',$sysInfo);
		} else{
			exit('站点已关闭');
		}

    }


}